#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (ch < '0' or ch > '9') f |= (ch == '-'), ch = getchar();
	while (ch >= '0' and ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int __stk[128], __top;
inline void write(int x) {
    if(x < 0) putchar('-'), x = -x;
	do { __stk[++__top] = x % 10, x /= 10; } while (x);
	while (__top) putchar(__stk[__top--] + '0');
}

const int mod = 998244353;

void Min(int &x, int y) { y < x and (x = y); }
void Max(int &x, int y) { y > x and (x = y); }

void inc(int &x, int y) { (x += y) >= mod and (x -= mod); }
void mul(int &x, int y) { x = 1ll * x * y % mod; }

int q_pow(int x, int k) { int res = 1; for (; k; k >>= 1, mul(x, x)) if (k & 1) mul(res, x); return res; }

bool stmer;

const int N = 8010, M = 6e4 + 10;

int n, m, ans;

struct node {
    int x, y, w;
} a[N];

int cnt;
int pri[M];

bool vis[M];

void init() {
	vis[1] = 1;
	for (int i = 2; i < M; i++) {
		if (!vis[i]) pri[++cnt] = i;
		for (int j = 1; i * pri[j] < M; j++) {
			int to = i * pri[j]; vis[to] = 1;
			if (!(i % pri[j])) break;
		}
	}
	for (int i = 5; i <= cnt; i++) if (pri[i] % 20 != 3) vis[pri[i]] = 1;
}

int dis(node x, node y) {
	return abs(x.x - y.x) + abs(x.y - y.y);
}

void work(int i, int j) {
	int d = dis(a[i], a[j]), val = a[i].w + a[j].w;
	for (int k = 1; k <= n; k++) if (i != k and j != k) {
		int x = dis(a[i], a[k]), y = dis(a[j], a[k]);
		if (min({ x, y, d }) == min(x, y) and max({ x, y, d }) == max(x, y)) {
			if (x == d and k < j) continue;
			if (y == d and k < i) continue;
			ans += a[k].w + val;
		}
	}
}

void solve() {
    ans = 0, n = read(), m = read();
    for (int i = 1; i <= n; i++) a[i] = { read(), read(), 0 };
    for (int i = 1; i <= n; i++) a[i].w = read();

    for (int i = 1; i <= n; i++) 
		for (int j = 1; j < i; j++) if (!vis[dis(a[i], a[j])]) work(i, j);

	write(ans), putchar('\n');
}

bool edmer;
signed main() {
	freopen("chess.in", "r", stdin);
	freopen("chess.out", "w", stdout);
	cerr << "[Memory] " << (&stmer - &edmer) / 1024 / 1024 << " MB\n";
	
	init();

    int T = read();
    while (T--) solve();

    cerr << "[Runtime] " << (double) clock() / CLOCKS_PER_SEC << " seconds\n";
	return 0;
} 